package com.factory.test.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class FileUtils {

    public static String readDevicesConfigFile(File file){
        StringBuffer sb = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line = null;
            while ((line = br.readLine()) != null){
                int index = line.indexOf("//");
                if(index > 0){
                    line = line.substring(0, index);
                }
                sb.append(line);
            }
            br.close();
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return sb.toString();
    }


    public static String readFile(String path) {
        File f = new File(path);
        if(!f.exists()){
            return null;
        }
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = bufferedReader.readLine()) != null){
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            Log.e("file", "文件读取失败", e);
        } finally {
            try {
                if (null != bufferedReader) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void writeFile(String filePath, String content) {
        try {
            File file = new File(filePath);
            File pf = file.getParentFile();
            if (!pf.exists()) {
                pf.mkdirs();
            }

            FileOutputStream fos = new FileOutputStream(file);
            fos.write(content.getBytes(StandardCharsets.UTF_8));
            fos.flush();
            fos.getFD().sync();
            fos.close();
            Log.i("file", "文件已保存 " + file.getName());
        } catch (Exception e) {
            Log.e("file", "文件保存失败", e);
        }
    }


    public static String getFilesPath(Context context) {
        String filePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            //外部存储可用
            filePath = context.getExternalFilesDir(null).getPath();
        } else {
            //外部存储不可用
            filePath = context.getFilesDir().getPath();
        }
        return filePath;
    }

    public static boolean copyFile(File sourceFile, File targetFile){
        try {
            FileInputStream fosfrom = new FileInputStream(sourceFile);
            FileOutputStream fosto = new FileOutputStream(targetFile);
            byte bt[] = new byte[1024];
            int c = 0;
            while ((c = fosfrom.read(bt)) > 0) {
                fosto.write(bt, 0, c); //将内容写到新文件当中
            }
            fosfrom.close();
            fosto.flush();
            fosto.close();
            return true;
        } catch (FileNotFoundException e) {
            return false;
        } catch (IOException e) {
            return false;
        }
    }


    public static String saveBitmapToPath(Context context, Bitmap bitmap){
        String rootPath = FileUtils.getFilesPath(context);
        String filePath = rootPath + "/faceList";

        File file = new File(filePath);
        if(!file.exists()){
            file.mkdirs();
        }

        File target = new File(filePath,  System.currentTimeMillis() + ".jpg");

        try {
            FileOutputStream fos = new FileOutputStream(target);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100,  fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        return target.toString();
    }


    public static String copyFileToPath(Context context, String take){
        String rootPath = FileUtils.getFilesPath(context);
        String filePath = rootPath + "/faceList";
        File target = new File(filePath,  System.currentTimeMillis() + ".jpg");
        FileUtils.copyFile(new File(take), target);
        return target.toString();
    }


    public static String copyFileToPath(Context context, InputStream source){
        String rootPath = FileUtils.getFilesPath(context);
        String filePath = rootPath + "/faceList";
        File targetFile = new File(filePath,  System.currentTimeMillis() + ".jpg");

        try {
            FileOutputStream fosto = new FileOutputStream(targetFile);
            byte bt[] = new byte[1024];
            int c = 0;
            while ((c = source.read(bt)) > 0) {
                fosto.write(bt, 0, c); //将内容写到新文件当中
            }
            source.close();
            fosto.flush();
            fosto.close();
            return targetFile.toString();
        } catch (FileNotFoundException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }

    public static String readFile2String(String path) {
        BufferedReader bufferedReader = null;
        FileReader fileReader = null;
        File f = new File(path);
        if(!f.exists()) return "";

        StringBuffer sb = new StringBuffer();
        try {
            fileReader = new FileReader(path);
            bufferedReader = new BufferedReader(fileReader);
            String value = null;
            while ((value = bufferedReader.readLine()) != null){
                sb.append(value);
            }
        } catch (Exception e) {
            Log.e("flie", "文件读取失败", e);
            return null;
        } finally {
            try {
                if (null != bufferedReader) {
                    bufferedReader.close();
                }
                if (null != fileReader) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}
